package com.hns.can.master.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hns.can.base.entity.SysAuthorityResourceEntity;
import com.hns.can.base.entity.SysRoleEntity;
import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.constants.GlobalConst;
import com.hns.can.common.entity.BaseEntity;
import com.hns.can.common.formula.FillCodeDscValue;
import com.hns.can.common.formula.FillParamsVo;
import com.hns.can.master.entity.BaseCarInfoEntity;
import com.hns.can.master.entity.BaseLineInfoEntity;
import com.hns.can.master.vo.LinePostDataVo;
import com.hns.frame.enumrate.EnabledSt;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.UtilString;

/**
 * 线路信息维护业务类
 * @author:lan
 * @email:763836737@qq.com
 * @version Revision 2.0.0
 */
@Service
public class LineInfoService extends CommonService {
    
	/**
	 * 填充代码描述
	 */
    @Autowired
    private FillCodeDscValue codeDscValue;
    
    
    /**
     * 添加一个线路
     * @param entity
     * @param user
     * @author:lan
     * @email:763836737@qq.com
     */
    @SuppressWarnings("unchecked")
    @Transactional
    @CacheEvict(value=GlobalConst.CACHE_KEY_BASE_LINE_INFO, key="#user.getCorpId()")
    public void addLineInfo(BaseLineInfoEntity entity, SysUserEntity user) {
        // 补充实体的其他信息
    	String lineCode=entity.getLineCode().trim();//去除空格
    	entity.setLineCode(lineCode);
        entity.setLineId(UtilString.getUUID());
        entity.setEnabledSt(EnabledSt.ENABLE.getFlag());  // 启用状态
        entity.setCrtTime(new Date());
        entity.setCrtUser(user.getAcctName()); 
        int counLine = getlineInfoByLineCode(entity);
        int countName = getlineInfoByLineName(entity);
        if(counLine>0 || countName>0){
        	throw new BusinessException(MsgType.ERROR.getFlag(), "该车辆已经添加到其他线路中!");
        }else{
        	 commonEntityDao.save(entity);
        }
       
        // 对应增加当前用户的角色及其上级角色的数据权限 upt by jeffxu 150327
        List<SysRoleEntity> list = PubUtil.convertListMapToObject(
            this.sqlMapDao.selectList("sysUser.getAllRoleByUserId", user.getId()), SysRoleEntity.class);
        for(SysRoleEntity role : list){
            SysAuthorityResourceEntity resEntity = new SysAuthorityResourceEntity();
            resEntity.setId(UtilString.getUUID());
            resEntity.setRoleId(role.getId());
            resEntity.setResDataId(entity.getLineId());
            resEntity.setCreateTs(new Date());
            resEntity.setIsFull("1");
            resEntity.setResType("RES00001");
            commonEntityDao.save(resEntity);
        }
    }
    
    /**
     * 得到线路page对象
     * @param dataVo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @Transactional
    public Pagination searchLineInfoPage(LinePostDataVo dataVo) throws Exception {
        Pagination page = this.myBatisPage("lineInfo.searchLineInfoCnt", "lineInfo.searchLineInfoList", dataVo,BaseLineInfoEntity.class);
        Map<String,String> map = new HashMap<String,String>();
        map.put("organName", GlobalConst.DEFAULT_CACHE_VALUE);   
        FillParamsVo vo = new FillParamsVo("organId", map);
        codeDscValue.fillInfoValue(page.getData(), new String[]{GlobalConst.CACHE_KEY_BASE_ORGAN}, vo);
        return page;
    }
    
    /**
     * 得到线路信息list对象
     * @param user
     * @param dataVo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @SuppressWarnings({"unchecked" })
    @Transactional
    public List<BaseEntity> getLineInfoList(LinePostDataVo dataVo) throws Exception{
        List<BaseEntity> list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("lineInfo.searchLineInfoList", dataVo), BaseLineInfoEntity.class);
        Map<String,String> mapTmp = new HashMap<String,String>();
        mapTmp.put("lineTypeId", "LIC00000");
        mapTmp.put("wthrUnmannedTicket", "WHT00000");
        codeDscValue.fillCodeDscValue(list, mapTmp);
        Map<String,String> map = new HashMap<String,String>();
        map.put("organName", GlobalConst.DEFAULT_CACHE_VALUE);   
        FillParamsVo vo = new FillParamsVo("organId", map);
        codeDscValue.fillInfoValue(list, new String[]{GlobalConst.CACHE_KEY_BASE_ORGAN}, vo);
        return list;
    }
    
    /**
     * 根据线路id求线路信息
     * @param lineId
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @Transactional
    public BaseLineInfoEntity getLineInfoByLineId(String lineId) throws Exception {
        BaseLineInfoEntity entity = commonEntityDao.find(lineId, BaseLineInfoEntity.class);
        List<BaseEntity> list = new ArrayList<BaseEntity>();
        list.add(entity);
        Map<String,String> map = new HashMap<String,String>();
        map.put("corpName", GlobalConst.DEFAULT_CACHE_VALUE);   
        FillParamsVo vo = new FillParamsVo("corpId", map);
//        codeDscValue.fillInfoValue(list, GlobalConst.CACHE_KEY_SYS_CORP);
        Map<String,String> map2 = new HashMap<String,String>();
        map2.put("organName", GlobalConst.DEFAULT_CACHE_VALUE);   
        FillParamsVo vo2 = new FillParamsVo("organId", map2);
        codeDscValue.fillInfoValue(list, new String[]{GlobalConst.CACHE_KEY_SYS_CORP, GlobalConst.CACHE_KEY_BASE_ORGAN}, vo, vo2);
        return entity;
    }
    
    /**
     *  更新一个线路的信息
     * @param user
     * @param entity
     * @author:lan
     * @email:763836737@qq.com
     */
    @Transactional
    @CacheEvict(value=GlobalConst.CACHE_KEY_BASE_LINE_INFO, key="#user.getCorpId()")
    public void uptLineInfo(SysUserEntity user, BaseLineInfoEntity entity){
    	String lineCode=entity.getLineCode().trim();//去除空格
    	entity.setLineCode(lineCode);
        entity.setEnabledSt(EnabledSt.ENABLE.getFlag());
        entity.setUpdateTime(new Date());
        entity.setUpdateUser(user.getAcctName());
        int counLine = getlineInfoByLineCode(entity);
        int countName = getlineInfoByLineName(entity);
        if(counLine>0 || countName>0){
        	throw new BusinessException(MsgType.ERROR.getFlag(), "您选择的线路编号已经存在!");
        }else{
        	commonEntityDao.update(entity);
        }
    }
    
    /**
     * 删除一个线路
     * @param user
     * @param lineId
     * @author:lan
     * @email:763836737@qq.com
     */
    @Transactional
    @CacheEvict(value=GlobalConst.CACHE_KEY_BASE_LINE_INFO, key="#user.getCorpId()")
    public void delLineInfo(SysUserEntity user, String[] lineIds){
        for (String lineId : lineIds) {
            BaseLineInfoEntity entity = commonEntityDao.find(lineId, BaseLineInfoEntity.class);
            entity.setEnabledSt(EnabledSt.DISABLE.getFlag());
            entity.setUpdateTime(new Date());
            entity.setUpdateUser(user.getAcctName());
            commonEntityDao.update(entity);
            // 关联删除的操作
            // 删除数据权限 upt by jeffxu 150326
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("resDataId", lineId);
            commonEntityDao.deleteByParamsIgnoreNullValue(SysAuthorityResourceEntity.class, map);
        }
    }
    
    /**
     * 查询是否数据库是否有相同线路编号
     * @param entity
     * @return
     * @author:lan
     * @email:763836737@qq.com
     */
    public int getlineInfoByLineCode(BaseLineInfoEntity entity) {
        int count = (Integer) sqlMapDao.selectOne("lineInfo.getlineInfoByLineCode", entity);
        return count;
    }
    
    /**
     * 查询是否数据库是否有相同线路名称
     * @param entity
     * @return
     * @author:lan
     * @email:763836737@qq.com
     */
    public int getlineInfoByLineName(BaseLineInfoEntity entity) {
        int count = (Integer) sqlMapDao.selectOne("lineInfo.getlineInfoByLineName", entity);
        return count;
    }
}
